package com.linkedlist;

/**
 * @BelongsProject: DataStructures
 * @BelongsPackage: com.linkedlist
 * @Author: 江岸
 * @CreateTime: 2020-11-21 19:46
 * @Description:
 */
public class DoubleLinkedList {
    public static void main(String[] args) {
        MyList myList = new MyList();
        myList.addByOrder(new HeroNode2(1, "宋江", "及时雨"));
        myList.addByOrder(new HeroNode2(2, "宋江", "及时雨"));
        myList.addByOrder(new HeroNode2(5, "宋江", "及时雨"));
        myList.addByOrder(new HeroNode2(3, "宋江", "及时雨"));
        //myList.show();
        //myList.update(new HeroNode2(2, "吴用", "智多星"));
        System.out.println("---------------");
        //myList.show();
       // myList.delete(3);
        myList.show();

    }
}

class HeroNode2{
    public int no;
    public String name;
    public String nickName;
    public HeroNode2 next;//指向下一个节点
    public HeroNode2 pre;//指向上一个节点

    public HeroNode2(int no, String name, String nickName) {
        this.no = no;
        this.name = name;
        this.nickName = nickName;
    }

    public HeroNode2() {
    }

    @Override
    public String toString() {
        return "HeroNode2{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickName='" + nickName + '\'' +
                '}';
    }
}

class MyList{
    private HeroNode2 head = new HeroNode2(0,"","");

    public HeroNode2 getHead() {
        return head;
    }

    //普通增加，找到最后节点，改一下next的指向即可
    public void add(HeroNode2 heroNode) {
        //因为head不能动，因此需要一个辅助变量temp
        HeroNode2 temp = head;
        //遍历链表，找到最后
        while (true) {
            if (temp.next == null) {
                break;
            }
            //如果没有找到最后，则将temp后移
            temp = temp.next;
        }
        temp.next = heroNode;
        heroNode.pre =temp;
    }

    //第二种添加方法，根据排名添加到指定位置(如果排名存在，则提示添加失败)
    public void addByOrder(HeroNode2 heroNode) {
        HeroNode2 temp = head;
        boolean flag = false;//表示添加的编号是否存在，默认为false
        while (true) {
            if (temp.next == null) {
                break;
            }
            if (temp.next.no > heroNode.no) {
                //找到地方，就在该temp后面插入
                break;
            } else if (temp.next.no == heroNode.no) {
                //编号已存在
                flag = true;
                break;
            }
            temp = temp.next;//后移即遍历
        }
        //判断flag
        if (flag) {
            System.out.printf("准备插入的英雄编号%d已经存在", heroNode.no);
        } else {
            //插入
            heroNode.next = temp.next;
            temp.next = heroNode;
            heroNode.pre = temp;
            if (heroNode.next !=null )heroNode.next.pre = heroNode;
        }


    }

    //修改
    public void update(HeroNode2 heroNode) {
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        HeroNode2 temp = head.next;
        boolean flag = false;
        while (true) {
            if (temp == null) break;
            if (temp.no == heroNode.no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            temp.name = heroNode.name;
            temp.nickName = heroNode.nickName;
        } else {
            System.out.println("没有这个编号的英雄");
        }
    }

    //删除
    public void delete(int no) {
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        HeroNode2 temp = head.next;
        boolean flag = false;
        while (true) {
            if (temp == null) {
                break;
            }
            if (temp.no == no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            temp.pre.next = temp.next;
            if (temp.next!=null) temp.next.pre = temp.pre;
        } else {
            System.out.println("没有这个编号的英雄");
        }

    }

    //遍历
    public void show() {
        HeroNode2 temp = head;
        while (true) {
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
            System.out.println(temp);
        }
    }
}
